package com.example.a2023110445

import android.annotation.SuppressLint
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import java.util.*

class Activity4 : AppCompatActivity() {
    
    private lateinit var timerTextView: TextView
    private lateinit var startButton: Button
    private lateinit var pauseButton: Button
    private lateinit var resetButton: Button
    
    private var handler = Handler(Looper.getMainLooper())
    private var isRunning = false
    private var elapsedTime = 0L
    private var startTime = 0L
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity4)
        
        timerTextView = findViewById(R.id.timerTextView)
        startButton = findViewById(R.id.startButton)
        pauseButton = findViewById(R.id.pauseButton)
        resetButton = findViewById(R.id.resetButton)
        
        // Restore state if available
        if (savedInstanceState != null) {
            elapsedTime = savedInstanceState.getLong("elapsedTime", 0L)
            isRunning = savedInstanceState.getBoolean("isRunning", false)
            startTime = savedInstanceState.getLong("startTime", 0L)
        }
        
        startButton.setOnClickListener {
            startTimer()
        }
        
        pauseButton.setOnClickListener {
            pauseTimer()
        }
        
        resetButton.setOnClickListener {
            resetTimer()
        }
        
        updateButtonStates()
        updateDisplay()
    }
    
    private fun startTimer() {
        if (!isRunning) {
            isRunning = true
            startTime = System.currentTimeMillis() - elapsedTime
            handler.post(updateTimer)
            updateButtonStates()
        }
    }
    
    private fun pauseTimer() {
        if (isRunning) {
            isRunning = false
            elapsedTime = System.currentTimeMillis() - startTime
            handler.removeCallbacks(updateTimer)
            updateButtonStates()
        }
    }
    
    private fun resetTimer() {
        isRunning = false
        elapsedTime = 0L
        startTime = 0L
        handler.removeCallbacks(updateTimer)
        updateDisplay()
        updateButtonStates()
    }
    
    private val updateTimer = object : Runnable {
        override fun run() {
            if (isRunning) {
                elapsedTime = System.currentTimeMillis() - startTime
                updateDisplay()
                handler.postDelayed(this, 10)
            }
        }
    }
    
    @SuppressLint("SetTextI18n")
    private fun updateDisplay() {
        val minutes = (elapsedTime / 60000) % 60
        val seconds = (elapsedTime / 1000) % 60
        val milliseconds = (elapsedTime / 10) % 100
        timerTextView.text = String.format(
            Locale.getDefault(),
            "%02d:%02d:%02d",
            minutes,
            seconds,
            milliseconds
        )
    }
    
    private fun updateButtonStates() {
        startButton.isEnabled = !isRunning
        pauseButton.isEnabled = isRunning
    }
    
    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putLong("elapsedTime", elapsedTime)
        outState.putBoolean("isRunning", isRunning)
        outState.putLong("startTime", startTime)
    }
    
    override fun onDestroy() {
        super.onDestroy()
        handler.removeCallbacks(updateTimer)
    }
}

